﻿
using STT.DataAccess;
using STT.Entities;
using System;
using System.Data;
using System.Data.SqlClient;

namespace STT.Business
{
    public class BTbChat
    {
        public static DataSet SelectMessageForHocVien(string IdGui, string IdNhan)
        {
            SqlParameter[] pr = new SqlParameter[2];
            pr[0] = new SqlParameter(@"IdGui", IdGui);
            pr[1] = new SqlParameter(@"IdNhan", IdNhan);
            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "TbChat_SelectMessageForHocVien", pr);
            all.TableName = "Row";

            DataView dtview = new DataView(all);
            dtview.Sort = "ThoiGianGui ASC";

            DataSet ds = new DataSet();
            ds.Tables.Add(dtview.ToTable());
            return ds;
        }
        public static void Insert(ETbChat OTbChat)
        {
            SqlParameter[] pr = new SqlParameter[5];
            pr[0] = new SqlParameter(@"IdGui", OTbChat.IdGui);
            pr[1] = new SqlParameter(@"IdNhan", OTbChat.IdNhan);
            pr[2] = new SqlParameter(@"NoiDung", OTbChat.NoiDung);
            pr[3] = new SqlParameter(@"ThoiGianGui", OTbChat.ThoiGianGui);
            //pr[4] = new SqlParameter(@"DinhKem", OTbChat.DinhKem);
            pr[4] = new SqlParameter(@"TrangThai", OTbChat.TrangThai);
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure, "TbChat_Insert", pr);
        }
        public static void Delete(string IdGui,string IdNhan, string ThoiGianGui)
        {
            SqlParameter[] pr = new SqlParameter[3];
            pr[0] = new SqlParameter(@"IdGui", IdGui);
            pr[1] = new SqlParameter(@"IdNhan", IdNhan);           
            pr[2] = new SqlParameter(@"ThoiGianGui", ThoiGianGui);
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure, "TbChat_Delete", pr);
        }

        public static DataSet SelectMessageForManager(string IdGui, string IdNhan, string lastTime)
        {
            SqlParameter[] pr = new SqlParameter[3];
            pr[0] = new SqlParameter(@"IdGui", IdGui);
            pr[1] = new SqlParameter(@"IdNhan", IdNhan);
            pr[2] = new SqlParameter(@"lastTime", lastTime == "" ? (object)DBNull.Value : lastTime);
            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "TbChat_SelectMessageForManager", pr);
            all.TableName = "Row";

            DataView dtview = new DataView(all);
            //dtview.Sort = "ThoiGianGui ASC";

            DataSet ds = new DataSet();
            ds.Tables.Add(dtview.ToTable());
            return ds;
        }
        public static DataSet SelectMessageForHocVien(string IdGui, string IdNhan,string lastTime)
        {
            SqlParameter[] pr = new SqlParameter[3];
            pr[0] = new SqlParameter(@"IdGui", IdGui);
            pr[1] = new SqlParameter(@"IdNhan", IdNhan);
            pr[2] = new SqlParameter(@"lastTime", lastTime==""?(object)DBNull.Value :lastTime);

            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "TbChat_SelectMessageForHocVien", pr);
            all.TableName = "Row";

            DataView dtview = new DataView(all);
            //dtview.Sort = "ThoiGianGui ASC";

            DataSet ds = new DataSet();
            ds.Tables.Add(dtview.ToTable());
            return ds;
        } 

        public static DataTable SelectTinNhanMoiForManager(string IdNhan)
        {
            SqlParameter[] pr = new SqlParameter[1];            
            pr[0] = new SqlParameter(@"IdNhan", IdNhan);
            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "TbChat_SelectTinNhanMoiForManager", pr);

            return all;             
        }


        public static DataTable UpdateTrangThaiTin(/*string IdGui, */string IdNhan/*,DateTime ThoiGianGui*/)
        {
            SqlParameter[] pr = new SqlParameter[1];
            //pr[0] = new SqlParameter(@"IdGui", IdGui);
            pr[0] = new SqlParameter(@"IdNhan", IdNhan);
            //pr[2] = new SqlParameter(@"ThoiGianGui", ThoiGianGui);
            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "TbChat_UpdateTrangThaiTin", pr);

            return all;
        }
        public static DataSet SelectMessageForGiangVien(string IdGui, string IdNhan)
        {
            SqlParameter[] pr = new SqlParameter[2];
            pr[0] = new SqlParameter(@"IdGui", IdGui);
            pr[1] = new SqlParameter(@"IdNhan", IdNhan);
            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "TbChat_SelectMessageForGiangVien", pr);
            all.TableName = "Row";

            DataView dtview = new DataView(all);
            dtview.Sort = "ThoiGianGui ASC";

            DataSet ds = new DataSet();
            ds.Tables.Add(dtview.ToTable());
            return ds;
        } 
    }
}